import 'package:flutter/material.dart';
import '../../../../widget/createInterface.dart';
import '../../../../widget/paginationControls.dart';

import 'package:get/get.dart';
import '../../../../services/screenAdapter.dart';
import '../controllers/interface_controller.dart';

class InterfaceListView extends GetView {
  InterfaceListView({super.key});

  final InterfaceController controller = Get.find();

  Widget _buildTableHeader() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 12, horizontal: 8),
      decoration: BoxDecoration(
        color: Colors.grey[200],
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        children: [
          _buildHeaderCell('接口名称', flex: 3),
          _buildHeaderCell('请求方法', flex: 1),
          _buildHeaderCell('接口路径', flex: 4),
          _buildHeaderCell('接口分类', flex: 2),
          _buildHeaderCell('状态', flex: 2),
        ],
      ),
    );
  }

  Widget _buildHeaderCell(String text, {int flex = 1}) {
    return Expanded(
      flex: flex,
      child: Text(text, style: TextStyle(fontWeight: FontWeight.bold)),
    );
  }

  Widget _buildApiItemRow(item) {
    return Container(
      margin: EdgeInsets.only(bottom: 8),
      padding: EdgeInsets.symmetric(vertical: 12, horizontal: 8),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(color: Colors.black12, blurRadius: 2, offset: Offset(0, 1)),
        ],
      ),
      child: Row(
        children: [
          _buildDataCell(
            item.name,
            flex: 3,
            onTap: () {
              controller.changeCurrentViewIndex(
                1,
                controller.currentMenuId.value,
                item.id ?? 0,
              );
            },
          ),
          _buildDataCell(item.methodStr, flex: 1),
          _buildDataCell(item.path, flex: 4, isPath: true),
          _buildDataCell(item.menuName, flex: 2),
          _buildDataCell(item.statusStr, flex: 2,onTap: (){
            // controller.changeStatus(item.id ?? 0);
          }),
        ],
      ),
    );
  }

  Widget _buildDataCell(
    String text, {
    int flex = 1,
    bool isPath = false,
    Function()? onTap,
  }) {
    return Expanded(
      flex: flex,
      child: InkWell(
        onTap: onTap,
        child: Text(
          text,
          overflow: TextOverflow.ellipsis,
          style: isPath
              ? TextStyle(color: Colors.blue, fontFamily: 'monospace')
              : null,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(ScreenAdapter.width(10)),
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 分类标题 + 添加接口按钮
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Obx(() {
                return Text(
                  "共 ${controller.totalInterface} 个接口",
                  style: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                  ),
                );
              }),
              ElevatedButton(
                onPressed: () {
                  // 实现添加接口逻辑，比如弹出表单对话框
                  Get.dialog(CreateInterface());
                },
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue,
                  foregroundColor: Colors.white,
                  padding: const EdgeInsets.symmetric(
                    horizontal: 16,
                    vertical: 8,
                  ),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(4),
                  ),
                ),
                child: const Text("添加接口", style: TextStyle(fontSize: 12)),
              ),
            ],
          ),
          SizedBox(height: ScreenAdapter.height(16)),
          // 表头
          _buildTableHeader(),
          SizedBox(height: ScreenAdapter.height(8)),
          Obx(() {
            return controller.interfaceList.isNotEmpty
                ? Expanded(
                    child: ListView.builder(
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemCount:
                          controller.interfaceList.length + 1, // +1 用于底部加载更多组件
                      itemBuilder: (context, index) {
                        if (index < controller.interfaceList.length) {
                          return _buildApiItemRow(
                            controller.interfaceList[index],
                          );
                        } else {
                          return PaginationControls(
                            currentPage: controller.currentPage.value,
                            totalPages: controller.totalPages.value,
                            onPageChanged: controller.changePage,
                          );
                        }
                      },
                    ),
                  )
                : Center(child: Text("暂无数据"));
          }),
        ],
      ),
    );
  }
}
